package org.ls.petclinic.web;

import cn.hutool.core.util.ObjectUtil;
import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.ls.petclinic.dao.SpecialtiesDao;
import org.ls.petclinic.dao.VetSpecialtiesDao;
import org.ls.petclinic.dao.VetsDao;
import org.ls.petclinic.dao.impl.SpecialtiesDaoImpl;
import org.ls.petclinic.dao.impl.VetSpecialtiesDaoImpl;
import org.ls.petclinic.dao.impl.VetsDaoImpl;
import org.ls.petclinic.pojo.Pets;
import org.ls.petclinic.pojo.Specialties;
import org.ls.petclinic.pojo.VetSpecialties;
import org.ls.petclinic.pojo.Vets;
import org.ls.petclinic.pojo.vo.VetsVo;
import org.ls.petclinic.untils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 浪矢先生
 * @description
 * @create 2022-12-05 22:13
 */
@WebServlet(urlPatterns = "/getVets")
public class GetVets extends  BaseServlet {
    private VetsDao vetsDao=new VetsDaoImpl();
    private VetSpecialtiesDao vetSpecialtiesDao= new VetSpecialtiesDaoImpl();
    private SpecialtiesDao specialtiesDao=new SpecialtiesDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Gson gson = new Gson();
        List<VetsVo> result=null;
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");

        String vetName = req.getParameter("VetName");
        String specialtyId = req.getParameter("specialtyId");
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        List<Vets> vets=null;
        if (!ObjectUtil.isNull(vetName)){
            vets = vetsDao.getByName(vetName);
        }
        else if (!ObjectUtil.isNull(specialtyId)){
            //TODO 联表查
        }
        else {
           vets= vetsDao.getAll();
        }
        if (!ObjectUtil.isNull(vets)) {
          result =  getVetSpeciality(vets);
        }
        JdbcUtils.commitAndClose();
        resp.getWriter().write(String.valueOf(gson.toJson(result)));
    }

    /**
     * 获得兽医的专长
     */
    private   ArrayList<VetsVo>  getVetSpeciality(List<Vets> vets){
        ArrayList<VetsVo> vetsVos = new ArrayList<>();
        for (Vets vet : vets) {
            VetsVo vetsVo = new VetsVo();
            StringBuilder specialty=new StringBuilder();
            Long vetId = vet.getId();
            List<VetSpecialties> vetSpecialties = vetSpecialtiesDao.getByVetId(vetId);
            for (VetSpecialties vetSpecialty : vetSpecialties) {
                Specialties specialties = specialtiesDao.getById(vetSpecialty.getSpecialtyId());
                specialty.append(specialties.getName()).append(",");

            }
            vetsVo.setId(vet.getId());
            vetsVo.setName(vet.getName());
            //去除最后一个逗号并返回
            vetsVo.setSpecialty(specialty.deleteCharAt(specialty.length()-1).toString());
            vetsVos.add(vetsVo);
        }
        return vetsVos;
    }
}
